#include <iostream>
#include <set>
#include <vector>
#include <cmath>
#include <numeric>

using namespace std;

static bool palindrome(uint64_t num) {
    uint64_t reversed = 0;
    uint64_t original = num;
    while (num) {
        reversed = reversed * 10 + num % 10;
        num /= 10;
    }
    return original == reversed;
}

int main() {
    constexpr auto target = static_cast<uint64_t>(1e8);
    set<uint64_t> s;
    for (uint64_t i = 2; i < target; ++i) {
        if (palindrome(i))
            s.insert(i);
    }

    vector<uint64_t> v;
    constexpr auto limit = static_cast<uint64_t>(sqrt(target *.5 - .25) - .5);
    for (uint64_t i = 1; i <= limit; ++i)
        v.push_back(i * i);
    
    set<uint64_t> a;
    for (uint64_t i = 2; i <= v.size(); ++i) {
        uint64_t n = accumulate(v.begin(), v.begin() + i, 0);
        if (n > *s.rbegin())
            break;
        else if (s.find(n) != s.end())
            a.insert(n);

        for (uint64_t j = i; j < v.size(); ++j) {
            n -= v[j - i];
            n += v[j];
            if (n > *s.rbegin())
                break;
            else if (s.find(n) != s.end())
                a.insert(n);
        }
    }
    cout << accumulate(a.begin(), a.end(), 0ULL) << endl;
}